package com.ruoyi.graph.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.graph.domain.TableNode;
import com.ruoyi.graph.domain.dto.TableNodeDTO;
import com.ruoyi.graph.service.TableNodeService;
import com.ruoyi.graph.service.TableRelationshipService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "血缘关系接口")
@RequestMapping("/tableNode")
@RestController
public class TableNodeController {

    @Autowired
    private TableNodeService tableNodeService;
    @Autowired
    private TableRelationshipService tableRelationshipService;


    @ApiOperation(value = "查询节点接口")
    @GetMapping(value = "/getNode")
    @ResponseBody
    @CrossOrigin
    public R getNode(@RequestParam("database")String database, @RequestParam("tableName")String tableName){
        TableNode tableNode = new TableNode();
        tableNode.setDatabase(database);
        tableNode.setTableName(tableName);
        return R.ok(tableNodeService.getNode(tableNode));
    }

    @ApiOperation(value = "创建节点接口")
    @PostMapping(value = "/save")
    @ResponseBody
    @CrossOrigin
    public R addNode(@RequestBody TableNodeDTO tableNodeDto){
        TableNode tableNode = new TableNode();
        BeanUtils.copyProperties(tableNodeDto, tableNode);
        tableNodeService.addNode(tableNode);
        return R.ok(true);
    }


    @ApiOperation(value = "查询所有节点及关系")
    @GetMapping(value = "/getAll")
    @ResponseBody
    @CrossOrigin
    public R getAll(){

        return R.ok(tableNodeService.getAllNodes());
    }


    @ApiOperation(value = "查询与该节点有关系的节点")
    @GetMapping(value = "/getAllNodesByName")
    @ResponseBody
    @CrossOrigin
    public R getAllNodesByName(@RequestParam("database")String database,@RequestParam("tableName")String tableName){
        TableNode tableNode = new TableNode();
        tableNode.setDatabase(database);
        tableNode.setTableName(tableName);
        return R.ok(tableNodeService.getAllNodesByName(tableNode));
    }


    @ApiOperation(value = "删除节点接口")
    @DeleteMapping(value = "/delete")
    @ResponseBody
    @CrossOrigin
    public R deleteNode(@RequestBody TableNodeDTO tableNodeDto){
        tableNodeService.deleteNode(tableNodeDto);
        return R.ok(true);
    }

    @ApiOperation(value = "创建节点关系接口")
    @PostMapping(value = "/saveRelation")
    @ResponseBody
    @CrossOrigin
    public R saveRelationShip(@RequestParam("sourceId")Long sourceId,@RequestParam("targetId")Long targetId,@RequestParam("action")String action){

        tableRelationshipService.createRelationship(sourceId,targetId,action);
        return R.ok(true);
    }

    @ApiOperation(value = "删除节点关系接口")
    @DeleteMapping(value = "/deleteRelation")
    @ResponseBody
    @CrossOrigin
    public R deleteRelationShip(@RequestParam("sourceId")Long sourceId,@RequestParam("targetId")Long targetId,@RequestParam("action")String action){

        tableRelationshipService.deleteRelationship(sourceId,targetId,action);
        return R.ok(true);
    }

}
